// pages/card/carddetl/carddetl.js
import Dialog from '@vant/weapp/dialog/dialog';
import Notify from '@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    active: "1",
    cardId: null,
    cardName: null,
    cardCount: null,
    recordList: [],
    recordMap: new Map(),
    show: false,
    actiTitleColumns: [],
    actiMap: [],
  },
  getScrollHeight() {
    // 获取内容区域的高度
    wx.createSelectorQuery().selectViewport().scrollOffset((res) => {
      this.setData({ scrollHeight: res.scrollHeight + 'px' });
    }).exec()
  },
  onScroll(event) {
    let isScrollEnd = false;
    if (event.detail.scrollTop > 10) {
      isScrollEnd = true;
    }
    if (isScrollEnd != this.data.isScrollEnd) {
      this.setData({ isScrollEnd: isScrollEnd })
    }
    this.setData({ scrollTop: event.detail.scrollTop });
  },
  // Tab页变动
  onChangeTab(event) {
    const recordList = this.data.recordMap.get(event.detail.name);
    this.setData({ recordList: recordList || [] })
    this.setData({ active: event.detail.name })
    this.getScrollHeight();
  },
  qryCardRecordList() {
    const param = {
      cardId: this.data.cardId,
      qryUserId: getApp().globalData.userInfo.userId
    }
    getApp().callContainer('/api/user/card/listRecord', param, (res) => {
      if (res.data.data.recordList) {
        // 转化为recordMap，key为type value为实体本身
        const recordMap = new Map();
        for (const record of res.data.data.recordList) {
          const key = record.type + "";
          if (recordMap.has(key)) {
            recordMap.get(key).push(record);
          } else {
            recordMap.set(key, [record]);
          }
        }
        const recordList = recordMap.get(this.data.active);
        this.setData({ recordList: recordList, recordMap: recordMap });
        this.getScrollHeight();
      }
    });
  },
  queryActiList() {
    getApp().callContainer('/api/actiQuery/list', {}, (res) => {
      if (res.data.data.actiList != null) {
        const actiTitleColumns = [];
        const actiMap = new Map();
        for (const acti of res.data.data.actiList) {
          actiTitleColumns.push(acti.actiTitle);
          actiMap.set(acti.actiTitle, acti.actiId);
        }
        this.setData({ actiTitleColumns: actiTitleColumns, actiMap: actiMap });
      }
    })
  },
  onClickToUseCard() {
    if (!this.data.cardCount || this.data.cardCount <= 0) {
      Toast('卡片数量不足');
      return;
    }
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false })
  },
  onConfirm(event) {
    const actiId = this.data.actiMap.get(event.detail.value);
    this.setData({ show: false })
    const message = "确定使用" + this.data.cardName + "吗？";
    Dialog.confirm({
      message: message,
    }).then(() => {
      const param = {
        cardId: this.data.cardId,
        actiId: actiId, 
        memo: '用户已使用（' + event.detail.value + '）'
      }
      getApp().callContainer('/api/user/card/use', param, (res) => {
        Notify({ type: 'success', message: '使用成功！', top: this.data.CustomBar });
        this.qryCardRecordList();
        this.setData({ active: "2", cardCount: this.data.cardCount - 1 });
      })
    }).catch(() => { });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      cardId: options.cardId,
      cardName: options.cardName,
      cardCount: options.cardCount
    });
    this.qryCardRecordList();
    this.queryActiList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})